package org.jeecg.modules.unifysys.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtil {

    private static final int SUCCESS_CODE = 200;
    public static String httpGet(String url, String param)
    {
        String result="";
        BufferedReader in =null;

        String realUrlstr = url+"?"+ param;

        try {
            URL realurl = new URL(realUrlstr);
            URLConnection connection = realurl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();

            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
	            /*for (String key : map.keySet()) {
	                 System.out.println(key + "--->" + map.get(key));
	             }*/
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }

        }

        return result;
    }


    public static JSONObject sendPost(String url, String nameValuePairs, String protool) throws IOException {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            if ("http".equals(protool)) {
                client = HttpClients.createDefault();
            } else {
                client = new SSLClient();
            }

            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(nameValuePairs, ContentType.create("application/json","UTF-8"));
            //设置请求内容

            //设置请求的报文的头部编码
            post.setHeader(new BasicHeader("Content-Type", "application/json; charset=UTF-8"));
            post.addHeader("Content-Type", "application/json; charset=utf-8");
            post.setEntity(entity);
            response = client.execute(post);
            int statuCode = response.getStatusLine().getStatusCode();
            if (SUCCESS_CODE == 200) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                jsonObject = JSONObject.parseObject(result);
                return jsonObject;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (KeyManagementException e) {
            e.printStackTrace();
            return null;
        } finally {
            response.close();
            client.close();
        }

        return null;
    }

    //参数类
    public static List<NameValuePair> getParam(Object[] params, Object[] values) {
        boolean flag = params.length > 0 && values.length > 0 && params.length == values.length;
        if (flag) {
            List<NameValuePair> nameValuePairs = new ArrayList<>();
            for (int i = 0; i < params.length; i++) {
                nameValuePairs.add(new BasicNameValuePair(params[i].toString(), values[i].toString()));
            }
            return nameValuePairs;
        } else {
//            log.error("参数有误，");
            System.out.println("参数有误");
        }

        return null;
    }

}
